exports.search_area_category = function (req, res) {

    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};
    var l_id = restfulParams.l_id == 0 || restfulParams.l_id == undefined ? "" :"/o_area/"+restfulParams.l_id;
    var tags = restfulParams.tags == 0 || restfulParams.tags == undefined ? "" :"/keywords/o_tags-"+ restfulParams.tags;
    var where = l_id + tags;

    var skip  =  restfulParams.skip == undefined  ? 0 : restfulParams.skip;

    var limit =  restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    //任务流创建
    async.waterfall([
        //查询该用户的订单
        function (cb) {
            //console.log(CORE_APIConfig.coreAPI.searchOrganization + where  + "/skip/"+skip+"/limit/" + limit);
            R.SEND_HTTP(

                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganization + where  + "/skip/"+skip+"/limit/" + limit,
                    method: 'POST',
                    data: {
                        fields: ["o_id","o_name","o_logo","o_tags","o_address","o_course_sales","o_longitude","o_latitude"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.organizationList = data.data;
                    cb();
                }
            );

        },

        //计算和自己的距离
        function(cb){

            for(var i = 0;i<resultData.organizationList.length;i++){

                var tempLng = resultData.organizationList[i].o_longitude;
                var tempLat = resultData.organizationList[i].o_latitude;
                var distance = (common.GetDistance(tempLat,tempLng,restfulParams.lat,restfulParams.lng)).toFixed(1);    
                resultData.organizationList[i].distance = distance;

            }

            cb();

        },


        //查询每个结构是否有约课课程
        function(cb){

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.organizationList.forEach(function (item, i) {
           
                //创建每一个插入任务
                tasks.push(function (cb) {
                    
                    //查询机构最新的一条约课课程信息
                    
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_organization_course + "/oc_oid/ " + item.o_id + "/oc_type/1/skip/0/limit/1",
                            method: 'POST',
                            data: {
                                fields: ["oc_id","oc_title","oc_course_count","oc_new_price"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }
    
                            item.courseData = data.data;
    
                            cb();
                        }
                    );

                });

            });

            async.waterfall(tasks, cb);

        }

    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, resultData);

    });

}